﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace HYCodeBase.AsyncUtility.AsyncSupport
{
	internal class SplitAndAllWaitHalfNSquareSupport : HalfNSquareSupport
	{
		public SplitAndAllWaitHalfNSquareSupport(IList<IElement> inputLstElement, int? useThreadCount) : base(inputLstElement, useThreadCount)
		{
		}

		public override void Calculate()
		{
			TaskFactory factory = Task.Factory;
			int nUseCoreNumber = this.m_nUseCoreNumber;
			int num = this.m_lstUseElment.Count / nUseCoreNumber;
			num = System.Math.Max(1, num);
			List<Task> list = new List<Task>();
			for (int i = 0; i < this.m_lstUseElment.Count; i += num)
			{
				int endIndex = System.Math.Min(this.m_lstUseElment.Count, i + num);
				SplitAndAllWaitHalfNSquareSupport.SupportBean supportBean = new SplitAndAllWaitHalfNSquareSupport.SupportBean();
				supportBean.StartIndex = i;
				supportBean.EndIndex = endIndex;
				list.Add(factory.StartNew(new Action<object>(this.SubCalculate), supportBean));
			}
			Task.WaitAll(list.ToArray());
		}

		private void SubCalculate(object inputBean)
		{
			SplitAndAllWaitHalfNSquareSupport.SupportBean supportBean = inputBean as SplitAndAllWaitHalfNSquareSupport.SupportBean;
			for (int i = supportBean.StartIndex; i < supportBean.EndIndex; i++)
			{
				base.CalculateByOneRow(i);
			}
		}

		private class SupportBean
		{
			internal int StartIndex
			{
				get
				{
					return this.m_nStartIndex;
				}
				set
				{
					this.m_nStartIndex = value;
				}
			}

			internal int EndIndex
			{
				get
				{
					return this.m_nEndIndex;
				}
				set
				{
					this.m_nEndIndex = value;
				}
			}

			private int m_nStartIndex;

			private int m_nEndIndex;
		}
	}
}
